var inquirer = require('inquirer');
const chalk = require('chalk');
const ora = require('ora');
const exec = require('child_process').exec;
const GIT_LIST = require('../templateList.js');
// var robot = require("robotjs"); // TODO: 安装不太方便，考虑移除 

function init(){
    inquirer.prompt([
        {
            type:"list",
            message:"请选择Git项目",
            name:'git_url',
            choices: GIT_LIST.map(i=>{
                return {
                    'value': i.value,
                    'name': i.name+chalk.hex('#3089DC')(i.desc)
                }
            }),
            default:"",
            pageSize:10,
        },
        {
            type:"input",
            message:"请输入项目文件夹名称",
            name:'git_name',
            default:"my project",
        }
    ]).then(async answers=>{
        let { git_url, git_name } = answers;
        const spinner = ora(chalk.green(`Start Clone...\n`));
        spinner.start();
        let cmdStr = `git clone ${git_url} ${git_name}`;
        exec(cmdStr, (error, stdout, stderr) => {
            if (error) {
                spinner.fail(chalk.red(error))
                process.exit()
            }
            spinner.succeed(chalk.green(`Clone completed!√\n`));
            exec(`cd ${git_name} && open README.md`,function(error){
                setTimeout(() => {
                    // robot.keyTap("v", ["command","shift"]);
                }, 500);
            })  
            
        })
    })
}
module.exports = init;